<template>
  <el-dialog
      :title="type == 'add' ? '添加供应商信息' : '修改供应商信息'"
      v-model="state.visible"
      width="400px"
  >
    <el-form :model="state.ruleForm" :rules="state.rules" ref="formRef" label-width="100px" class="good-form">
      <el-form-item label="供应商名" prop="supplierName">
        <el-input type="text" v-model="state.ruleForm.supplierName"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="supplierPhone">
        <el-input type="tel" v-model="state.ruleForm.supplierPhone"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import {reactive, ref} from 'vue';
import {localGet} from '@/utils';
import {ElMessage} from 'element-plus';
import {addOnePermission, permissionDetail, permissionUpdate} from "@/api/permission";
import {supplierAdd, supplierDetail, supplierUpdate} from "@/api/supplier";

const props = defineProps({
  type: String,
  reload: Function
});

const formRef = ref(null);
const state = reactive({
  token: localGet('token') || '',
  visible: false,
  ruleForm: {
    supplierId: '',
    supplierName: '',
    supplierPhone: '',
    createTime: '',
    updateTime: ''
  },
  rules: {
    supplierName: [
      {required: 'true', message: '供应商名不能为空', trigger: ['change']}
    ],
    supplierPhone: [
      {required: 'true', message: '联系方式不能为空', trigger: ['change']}
    ]
  },
  id: ''
});
// 获取详情
const getDetail = async (supplierId) => {
  console.log('详情', supplierId);
  await supplierDetail(supplierId).then((res) => {
    console.log('当前数据', res.data.data)
    state.ruleForm = res.data.data;
  }).catch(e => {
    console.log('请求失败！', e);
  })

}
// 开启弹窗
const open = (row) => {
  state.visible = true;
  console.log('当前行', row);
  console.log('当前操作', props.type);
  if (row) {
    console.log('载入行数据，{}', row);
    getDetail(row.supplierId);
  } else {
    state.ruleForm = {
      permissionId: '',
      permissionName: '',
      permissionChar: '',
      createTime: ''
    };
  }
};
// 关闭弹窗
const close = () => {
  state.visible = false;
  state.ruleForm = {
    permissionId: '',
    permissionName: '',
    permissionChar: '',
    createTime: ''
  };
};
const submitForm = () => {
  console.log(formRef.value.validate);
  formRef.value.validate(async (valid) => {
    if (valid) {
      if (props.type == 'add') {
        console.log('添加操作');
        supplierAdd(state.ruleForm).then(() => {
          ElMessage.success('添加成功');
          state.visible = false;
          if (props.reload) props.reload();
        });
      } else {
        console.log('修改操作');
        await supplierUpdate(state.ruleForm).then((res) => {
          ElMessage.success('修改成功');
        })
        close()
        if (props.reload) props.reload();

      }
    }
  });
};
defineExpose({open, close});
</script>


<style scoped>
.avatar-uploader {
  width: 100px;
  height: 100px;
  color: #ddd;
  font-size: 30px;
}

.avatar-uploader >>> .el-upload {
  width: 100%;
  text-align: center;
}

.avatar-uploader-icon {
  display: block;
  width: 100%;
  height: 100%;
  border: 1px solid #e9e9e9;
  padding: 32px 17px;
}
</style>